package br.fatecsp.ihc.pedidos.service;

import java.util.Collection;

import javax.persistence.EntityManager;

import br.fatecsp.ihc.pedidos.model.Cliente;

public class ClienteServiceImpl implements ClienteService {
    @SuppressWarnings("unchecked")
    public Collection<Cliente> findAll() {
        EntityManager em = EntityManagerProvider.getEntityManagerFactory().createEntityManager();
        try {
            return em.createNamedQuery("Cliente.findAll").getResultList();
        } finally {
            em.close();
        }
    }

    @SuppressWarnings("unchecked")
    public Collection<Cliente> findAllClientesWithNome(String nome) {
        EntityManager em = EntityManagerProvider.getEntityManagerFactory().createEntityManager();
        try {
            return em.createNamedQuery("Cliente.findAllClientesWithNome").setParameter("nome", "%" + nome + "%").getResultList();
        } finally {
            em.close();
        }
    }

    public Cliente findClienteById(Long id) {
        EntityManager em = EntityManagerProvider.getEntityManagerFactory().createEntityManager();
        try {
            return em.find(Cliente.class, id);
        } finally {
            em.close();
        }

    }
}
